package com.corechina.apex_osswords.common;

import com.corechina.apex_osswords.common.config.oauth2.OAuth2Filter;
import com.corechina.apex_osswords.common.config.oauth2.OAuth2Realm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.Cookie;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Shiro配置
 *
 * @author cc
 */
@Configuration
public class ShiroConfig {

    @Bean("sessionManager")
    public SessionManager sessionManager() {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setSessionValidationSchedulerEnabled(true);
        sessionManager.setSessionIdCookieEnabled(true);
        Cookie c = new SimpleCookie("wms.session.id");
        //10秒失效
        c.setMaxAge(10);
        sessionManager.setSessionIdCookie(c);
        return sessionManager;
    }

    @Bean("securityManager")
    public SecurityManager securityManager(OAuth2Realm oAuth2Realm, SessionManager sessionManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(oAuth2Realm);
        securityManager.setSessionManager(sessionManager);

        return securityManager;
    }

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);
        //oauth过滤
        Map<String, Filter> filters = new HashMap<>();
        filters.put("oauth2", new OAuth2Filter());
        shiroFilter.setFilters(filters);

        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/favicon.ico", "anon");
        filterMap.put("/webjars/**", "anon");
        filterMap.put("/static/**", "anon");
        filterMap.put("/swagger/**", "anon");
        filterMap.put("/swagger-ui.html", "anon");
        filterMap.put("/swagger-resources", "anon");
        filterMap.put("/swagger-resources/**", "anon");
        filterMap.put("/v2/api-docs", "anon");
        filterMap.put("/webjars/springfox-swagger-ui/**", "anon");
        filterMap.put("/sys/verifyWX", "anon");
        filterMap.put("/sys/login/login", "anon");
        filterMap.put("/sys/login/getCode", "anon");
        filterMap.put("/sys/login/getVerifyCode", "anon");
        filterMap.put("/sys/login/checkVerifyCode", "anon");
        filterMap.put("/setting/system/getOne", "anon");
        filterMap.put("/**/*.css", "anon");
        filterMap.put("/**/*.js", "anon");
        filterMap.put("/**/*.html", "anon");
        filterMap.put("/*.txt", "anon");
        filterMap.put("/upload", "anon");
        filterMap.put("/ws/asset", "anon");
        filterMap.put("/images/**", "anon");
        filterMap.put("/", "anon");
        filterMap.put("OPTIONS", "anon");
        filterMap.put("/core/weiXinUser/sendCode", "anon");
        filterMap.put("/core/weiXinUser/getOpenId", "anon");
        filterMap.put("/core/weiXinUser/bindApplets", "anon");
        filterMap.put("/core/weiXinUser/loginByWeiXin", "anon");
        filterMap.put("/core/weiXinProject/getListForAll", "anon");
//        filterMap.put("/core/weiXinUser/getEngineer", "anon");
        filterMap.put("/core/workOrder/sendWeappMessage", "anon");
        filterMap.put("/core/visitor/getPhone", "anon");
        filterMap.put("/core/visitingRecords/add", "anon");
        filterMap.put("/core/place/getOne", "anon");
        filterMap.put("/core/weiXinProject/getEqmDetailsBySerialNumberForAll", "anon");
        filterMap.put("/core/weiXinProject/getOneBySerialNumberForAll", "anon");
        //全局拦截
//        filterMap.put("/**", "anon");
        filterMap.put("/**", "oauth2");
        shiroFilter.setFilterChainDefinitionMap(filterMap);

        return shiroFilter;
    }

    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
        proxyCreator.setProxyTargetClass(true);
        return proxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

}
